<template>
  <div class="app-container">
    <el-form :model="queryParams" :inline="true">
        <el-form-item label="分类" >
            <el-select
                class="query-el-input-width"
                v-model="queryParams.type"
                placeholder="请选择"
                clearable
                filterable
                style="width: 214px"
            >
                <el-option
                    v-for="item in classifyList"
                    :key="item.name"
                    :value="item.name"
                    :label="item.name"
                    class="query-el-input-width"
                />
            </el-select>
        </el-form-item>
        <el-form-item label="发布状态" >
            <el-select
                class="query-el-input-width"
                v-model="queryParams.status"
                placeholder="请选择"
                clearable
                style="width: 214px"
            >
                <el-option
                    v-for="(item,index) in ['待审核','发布']"
                    :key="item"
                    :value="index"
                    :label="item"
                    class="query-el-input-width"
                />
            </el-select>
        </el-form-item>
        <el-form-item label="身份" >
            <el-input
                v-model="queryParams.identity"
                placeholder="请输入"
                clearable
                @keyup.enter="handleQuery"
            />
        </el-form-item>
        <el-form-item label="发布人昵称" >
            <el-input
                v-model="queryParams.nickname"
                placeholder="请输入"
                clearable
                @keyup.enter="handleQuery"
            />
        </el-form-item>
        <el-form-item label="发布人ID" >
            <el-input
                v-model="queryParams.userId"
                placeholder="请输入"
                clearable
                @keyup.enter="handleQuery"
            />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="addhandle">新增</el-button >
      </el-col>
    </el-row>
    <el-table  class="newClass" v-loading="loading" :data="localLifeTableList">
        <el-table-column label="序号" align="center" prop="id" />
        <el-table-column label="江湖分类" align="center" prop="type" />
        <el-table-column label="内容" align="center" prop="content" >
            <template #default="scope">
                <div style="overflow: hidden;display: -webkit-box;-webkit-line-clamp: 1;-webkit-box-orient: vertical;">{{ scope.row.content }}</div>
            </template> 
        </el-table-column>
        <el-table-column label="图片" align="center" prop="bannerUrls" width="180" >
          <template #default="scope">
              <div style="display: flex;">
                  <el-image
                      v-for="(url,index) in scope.row.bannerUrls ? scope.row.bannerUrls.split(',') : 0" :key="url"
                      :preview-teleported="true"
                      :z-index="99999999"
                      :initial-index="index"
                      :preview-src-list="scope.row.bannerUrls.split(',')"
                      style="width: 100px; height: 100px" :src="url" fit="contain"
                  />
              </div>
          </template>
        </el-table-column>
        <el-table-column label="排序" align="center" prop="sortNum" />
        <el-table-column label="发布状态" align="center" prop="status">
            <template #default="scope">
                <span v-if=" scope.row.status == 0">待审核</span>
                <span v-if=" scope.row.status == 1">发布</span>
            </template>
        </el-table-column>
        <el-table-column label="发布人昵称" align="center" prop="nickname" />
        <el-table-column label="发布人ID" align="center" prop="userId" />
        <el-table-column label="身份" align="center" prop="identity" />
        <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
            <template #default="scope">
                <el-button link type="primary" icon="" @click="handleUpdate(scope.row)">编辑</el-button>
                <el-button link type="primary" icon="" @click="handleDelete(scope.row)">删除</el-button>
            </template>
        </el-table-column>
    </el-table> 
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog title="江湖帖子" v-model="open" width="700">
        <el-form :model="form" label-width="100px">
            <el-form-item label="发布人头像" prop="avatar">
                <image-uploads v-model="form.avatar" :limit="1"></image-uploads>
            </el-form-item>
            <el-form-item label="发布人昵称" prop="nickname">
                <el-input
                v-model="form.nickname"
                placeholder="发布人昵称"
                type="text"
                style="width: 215px"
                />
            </el-form-item>
            <el-form-item label="分类名称" prop="type">
                <el-select
                    class="query-el-input-width"
                    v-model="type"
                    placeholder="请选择"
                    clearable
                    filterable
                    style="width: 214px"
                    multiple
                    @change="selectChange"
                >
                    <el-option
                        v-for="item in classifyList"
                        :key="item.name"
                        :value="item.name"
                        :label="item.name"
                        class="query-el-input-width"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="内容" prop="content">
                <el-input
                v-model="form.content"
                placeholder="内容"
                type="textarea"
                style="width: 215px"
                />
            </el-form-item>
            <el-form-item label="图片" prop="bannerUrls">
                <image-uploads v-model="form.bannerUrls"></image-uploads>
            </el-form-item>
            <el-form-item label="排序" prop="sortNum">
                <el-input
                v-model="form.sortNum"
                placeholder="排序"
                style="width: 215px"
                />
            </el-form-item>
            <el-form-item label="是否推荐" prop="status">
               <el-radio-group v-model="form.status">
                    <el-radio label="0">待审核</el-radio>
                    <el-radio label="1">发布</el-radio>
                </el-radio-group>
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
            <el-button :loading="buttonLoading" :disabled="buttonLoading" type="primary" @click="submitForm">确定</el-button>
            <el-button @click="cancel">取 消</el-button>
            </div>
        </template>
    </el-dialog>
  </div>
</template>
<script setup name="JianghuPost">
import {ref} from "vue";
import {delPostRunescape,postRunescapeList,upPostRunescape,addPostRunescape,postClassifyList} from "@/api/business/newMiniProgram";
import ImageUploads from "@/components/ImageUploads/index.vue";
import { ElMessage } from 'element-plus'
const localLifeTableList = ref([]);
const loading = ref(true);
const buttonLoading = ref(false);
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
})
const { proxy } = getCurrentInstance();
const open = ref(false);
const form = ref({});
const total = ref(0);
const type = ref([]);
function selectChange(e){
    if (e.length > 2) {
        ElMessage.warning('最多只能选择两个选项')
        type.value.pop()
    }
}
function addhandle(){
    form.value = {}
    open.value = true
}
function handleUpdate(e){
    if(e.type){
        type.value = e.type.split(',')
    }
    form.value = JSON.parse(JSON.stringify(e))
    open.value = true
}
function submitForm(){
    buttonLoading.value = true
    form.value.userId = 1
    console.log(form.value.type)
    if(type.value.length>0){
        form.value.type = type.value.join(',')
    }
    if(form.value.id){
        upPostRunescape(form.value).then(res=>{
            if(res.code==200){
                proxy.$modal.msgSuccess("修改成功");
                getList()
                open.value = false
            }
        }).finally(()=>{
            buttonLoading.value = false
        })
    }else{
        addPostRunescape(form.value).then(res=>{
            if(res.code==200){
                proxy.$modal.msgSuccess("新增成功");
                getList()
                open.value = false
            }
        }).finally(()=>{
            buttonLoading.value = false
        })
    }
}
/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除Id为"' + row.id + '"的帖子？').then(function() {
    loading.value = true;
    return delPostRunescape(row.id);
  }).then(() => {
    loading.value = true;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}
/** 查询极客侠后台管理系统本地消耗列表 */
function getList() {
  loading.value = true;
  postRunescapeList(queryParams.value).then(response => {
    localLifeTableList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function cancel(e) {
  open.value = false
}
const classifyList = ref([])
function getPostClassifyList() {
  postClassifyList({
    pageNum:1,
    pageSize:100,
    type:'江湖'
  }).then(response => {
    classifyList.value = response.rows
  });
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
getList();
getPostClassifyList()
</script>

